package com.boot.down.controller;


import com.boot.down.common.Const;
import com.boot.down.entity.UserInfo;
import com.boot.down.service.UserInfoService;
import com.boot.down.util.CookieUtil;
import com.boot.down.util.JsonUtils;
import com.boot.down.util.JwtUtil;
import com.boot.down.util.generator.StringUtils;
import com.boot.down.util.message.CommonMessage;
import com.boot.down.util.wrapper.Result;
import com.boot.down.vo.UserTokenVO;
import com.boot.down.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * @author Administrator
 */
@RestController
@RequestMapping("/user")
public class UserLoginController {

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private RedisTemplate redisTemplate;

    @RequestMapping("/login")
    public UserTokenVO login(@RequestParam String userName,
                             @RequestParam String password) {
        UserTokenVO userTokenVO = new UserTokenVO();


        UserInfo user = userInfoService.login(userName, password);

        if (user != null) {
            userTokenVO.setUserName(user.getUserName())
                    .setLastLoginTime(user.getUpdateTime())
                    .setUserIdCard(user.getUserIdCard())
                    .setUserRole(user.getUserRole())
                    .setToken(JwtUtil.getToken(user.getUserName(), user.getUpdateTime()));
        }

        return userTokenVO;
    }


    @RequestMapping("/toLogin")
    public Result loginSession(HttpSession session,
                               @RequestParam String userName,
                               @RequestParam String password,
                               HttpServletResponse response) {

        UserInfo user = userInfoService.login(userName, password);
        UserVO userVO = new UserVO();
        BeanUtils.copyProperties(user, userVO);
        //存入Token
        CookieUtil.writeCookieInResponse(response, session.getId());
        redisTemplate.opsForValue().set(session.getId(), JsonUtils.obj2String(userVO), Const.EFFECTIVE_TIME, TimeUnit.SECONDS);
        return Result.createSuccess(userVO);
    }

    /**
     * 根据请求中底Token进行取值
     *
     * @param request 请求信息
     * @return 返回结果
     */
    @RequestMapping("/getUser")
    public Result getUserInfo(HttpServletRequest request) {
        String token = CookieUtil.readCookieInRequest(request);
        if (StringUtils.isEmpty(token)) {
            return Result.createFailure(CommonMessage.INVALID);
        }
        UserVO userVO = JsonUtils.string2Obj(Objects.requireNonNull(redisTemplate.opsForValue().get(token)).toString(), UserVO.class);
        return Result.createSuccess(userVO);
    }

    /**
     * 退出登录
     * @param request 请求信息
     * @param response 响应信息
     * @return 返回结果
     */
    @RequestMapping("/loginOut")
    public Result loginOut(HttpServletRequest request, HttpServletResponse response){

        String token = CookieUtil.readCookieInRequest(request);
        CookieUtil.delCookie(request,response);
        //删除缓存
        boolean flag = redisTemplate.delete(token);
        if(flag) {
            return Result.createSuccess(true, CommonMessage.OK);
        }
        return Result.createFailure(CommonMessage.OPERATION_FAILED);
    }


}
